package com.douyin.video.controller;

import com.app.utils.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.douyin.video.entity.DouyinVideo;
import com.douyin.video.service.DouyinVideoService;
import com.douyin.video.view.DouyinVideoView;
import com.sys.user.entity.SysUser;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 抖音视频
 *
 *
 * @author djianfeng
 * @since 2018-08-27
 */
@Controller
@RequestMapping("/douyinVideo")
public class DouyinVideoController {
	
	@Value("${staticUrl}")
	private String staticUrl;
	
	@Autowired
	DouyinVideoService douyinVideoService;

	@RequiresPermissions("DouyinVideoList")
	@RequestMapping(value = "/list.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> list(ServletRequest request, ServletResponse response, Page<DouyinVideoView> page,
			DouyinVideoView view) {
		Map<String, Object> map = new HashMap<>();
		Subject currentUser = SecurityUtils.getSubject();
		SysUser user= (SysUser) currentUser.getPrincipal();
		if(user!=null) {
			view.setUserId(user.getId());
		}
		page.setDescs("id");
		List<DouyinVideoView> list = douyinVideoService.getDouyinVideoViews(page, view);
		map.put("records", list);
		map.put("total", page.getTotal());
		request.setAttribute("staticUrl", staticUrl);
		return map;
	}

	@RequiresPermissions("DouyinVideo")
	@RequestMapping("/golist.do")
	public String tolist() {
		return "module/douyinvideo/list";
	}
	
	
	@RequiresPermissions("GetVideo")
	@RequestMapping("/getVideo.do")
	public String godownload() {
		return "module/douyinvideo/getVideo";
	}

	@RequiresPermissions("EditDouyinVideo")
	@RequestMapping("/goEdit.do")
	public ModelAndView goEdit(DouyinVideo entity) {
		Map<String, Object> map = new HashMap<>();
		if (entity.getId() != null) {
			entity = this.douyinVideoService.selectById(entity.getId());
		}
		map.put("entity", entity);
		return new ModelAndView("module/douyinvideo/edit", map);
	}

	@RequiresPermissions("EditDouyinVideo")
	@RequestMapping(value = "/save.do", method = RequestMethod.POST)
	@ResponseBody
	public Result save(DouyinVideo entity) {
		boolean n = this.douyinVideoService.insertOrUpdate(entity);
		return Result.returnResult(n);
	}

	@RequiresPermissions("DelDouyinVideo")
	@RequestMapping(value = "/del.do", method = RequestMethod.POST)
	@ResponseBody
	public Result del(DouyinVideo entity) {
		boolean n=douyinVideoService.delDouyinVideo(entity.getId());
		return Result.returnResult(n);
	}

	@RequiresPermissions("UpdateDouyinVideo")
	@RequestMapping(value = "/update.do", method = RequestMethod.POST)
	@ResponseBody
	public Result update(DouyinVideo entity) {
		boolean n = this.douyinVideoService.updateById(entity);
		return Result.returnResult(n);
	}
}
